var uid,status,_datasDatagrid, selectForm;

function addRow() {
	$.post(sy.bp()+"/admin/user/visitor/add", {uid:uid,status:status}, function(data) {
		if (data && data.result==1) {
			$.messager.show({msg:data.msg,title:'系统提示'});
			_datasDatagrid.datagrid('unselectAll');
			_datasDatagrid.datagrid('reload');
		} else {
			$.messager.alert('警告', data.msg, 'warning');
		}
	}, "json");
};

function deleteRow() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('请确认', '您确实要删除当前所有选择的记录吗？', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].id);
				}
				$.post(sy.bp()+"/admin/user/visitor/delete", {ids:ids.join(","),status:status}, function(data) {
					if (data && data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						_datasDatagrid.datagrid('unselectAll');
						_datasDatagrid.datagrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
};


function search_datas() {
	_datasDatagrid.datagrid('load',sy.serializeObject(selectForm));
}


function reset_datas() {
	createDate1.datebox('setValue', '');
	createDate2.datebox('setValue', '');
	_datasDatagrid.datagrid('load',{});
}
function rejectRow() {
	editRowsNumber = -1;
	_datasDatagrid.datagrid('rejectChanges');
	_datasDatagrid.datagrid('unselectAll');
};
$(function() {
	uid = $('#uid').val();
	status= $('#status').val();
	_datasDatagrid = $('#_datasDatagrid');
	selectForm = $('#selectForm');
	createDate1 = $('#createDate1').datebox({editable:false}).datebox('activeTextArrow');
	createDate2 = $('#createDate2').datebox({editable:false}).datebox('activeTextArrow');
	
	_datasDatagrid.datagrid({
		url : sy.bp()+'/admin/user/visitor/list?uid=' + uid+"&status=" +status,
		fit : true,
		autoRowHeight : true,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : false,
		fitColumns : true,
		pageSize : 10,
		pageList : [10,20,30,40,50],
		nowrap : true,
		border : false,
		idField : 'id',
		sortName : 'createTime',
		sortOrder : 'desc',
		view: sy.myview,
        emptyMsg: '没有查询到相关数据',
		toolbar: [
		          {text:'增加',iconCls: 'icon-add',handler: addRow},'-',
		          {text:'批量删除',iconCls: 'icon-remove',handler: deleteRow},'-',
		          {text:'撤销',iconCls: 'icon-redo',handler: rejectRow},'-',
		          {text:'刷新',iconCls: 'icon-reload',handler: function(){_datasDatagrid.datagrid('reload');}},'-'
		         ],
		frozenColumns: [[{field:'id',title:'编号',align:'center',width:100,sortable:true,checkbox:true}]],
		columns:[[
		          {field:'vname',title:'来访者',algin:'left',width:100},
		          {field:'createName',title:'昵称', align:'left',width:100,sortable:true},
		          {field:'vphone',title:'手机号',algin:'left',width:100},
		          {field:'vip',title:'IP地址',algin:'left',sortable:true,width:100},
		          {field:'createTime',title:'访问时间',align:'left',width:100,sortable:true}
				]],
		onLoadSuccess : function(data) {
			$.messager.progress('close');
		},
		onRowContextMenu : function(e, rowIndex, rowData){
			e.preventDefault();
			_datasDatagrid.datagrid('unselectAll').datagrid('selectRow', rowIndex);
			$('#datagridContextMenu').menu('show', {left:e.pageX,top:e.pageY});
		}
	});
});